import API from '../util/api'
import Http from '../util/net'

async function get({url, callback, after}) {
    let ret = await await Http.netFetch(url);
    if (ret.code === 200) {
        callback(ret.data);
    } else {
        weex.requireModule('modal').toast({
            message: '网络请求失败'
        })

    }
    after && after()
}

export const search_book = async ({query, start, limit, callback, after}) => {
    get({
        url: API.SEARCH + '?query=' + query + '&start=' + start + '&limit=' + limit,
        callback: callback,
        after: after
    });
};

export const book_info = async ({bookId, callback, after}) => {
    get({
        url: API.BOOK_INFO + bookId,
        callback: callback,
        after: after
    });

};

// 获取小说章节(根据小说id)
export const book_chapter_by_book_id = async ({bookId, callback, after}) => {
    get({
        url: API.BOOK_CHAPTER_BOOK_ID + bookId,
        callback: callback,
        after: after
    });
};

// 获取小说章节(根据小说源id)
export const book_chapter_by_source_id = async ({sourceId, callback, after}) => {
    get({
        url: API.BOOK_CHAPTER_SOURCE_ID + sourceId,
        callback: callback,
        after: after
    });
};

// 获取小说章节(根据小说源id)
export const book_chapter = async ({sourceId, bookId, callback, after}) => {
    get({
        url: API.BOOK_CHAPTER + "?sourceId=" + sourceId + "&bookId=" + bookId,
        callback: callback,
        after: after
    });
};

// 获取小说章节内容
export const book_content_by_chapter_link = async ({link, callback, after}) => {
    get({
        url: API.BOOK_CONTENT + "?link=" + encodeURIComponent(link),
        callback: callback,
        after: after
    });
};

// 获取主分类
export const sort_major_info = async ({callback, after}) => {
    get({
        url: API.MAJOR_SORT,
        callback: callback,
        after: after
    });
};

// 获取全部分类信息
export const sort_all_info = async ({callback, after}) => {
    get({
        url: API.All_SORT,
        callback: callback,
        after: after
    });
};

// 获取分类的书
/**
 *  gender: 男生:mael 女生:female 出版:press
    type: 热门:hot 新书:new 好评:repulation 完结: over
    major: 大类别
    minor: 小类别  (非必填)
    start: 分页开始页
    limit: 分页条数
 * @param callback
 * @param after
 * @returns {Promise<void>}
 */
// object 转 URL 参数
export const createQuery = (obj) => {
    if (obj === null || obj === "" || obj.length === 0) {
        return ""
    }
    let url = '?';
    for (let key in obj) {
        if (obj[key] !== null) {
            url += (key + '=' + encodeURIComponent(obj[key]) + '&');
        }
    }
    return url.substring(0, url.lastIndexOf('&'));
};

export const sort_book = async ({params, callback, after}) => {
    get({
        url: API.SORT_BOOK + createQuery(params),
        callback: callback,
        after: after
    });
};

// 获取排行分类
export const rank_info = async ({callback, after}) => {
    get({
        url: API.RANK_INFO,
        callback: callback,
        after: after
    });
};
// 获取排行的书，通过排行ID
export const rank_book = async ({params, callback, after}) => {
    get({
        url: API.RANK_BOOK + createQuery(params),
        callback: callback,
        after: after
    });
};

// 获取书架的内容
export const book_store = async ({params, callback, after}) => {
    get({
        url: API.BOOK_STORE + createQuery(params),
        callback: callback,
        after: after
    });
};

// 获取书架的更多内容
export const book_store_more = async ({params, callback, after}) => {
    get({
        url: API.BOOK_STORE_MORE + createQuery(params),
        callback: callback,
        after: after
    });
};